package java6703.lesson12;

import java.util.Random;
import java.util.stream.IntStream;

public class SimpleStream6703 {
    public static void main(String[] args) {

        //产生随机数
        Random random = new Random();

        //对随机生成一组40个[0,60]的整数其中的素数求和
        int sum = IntStream.generate(() -> random.nextInt(61)).limit(40)
                .distinct()
                .filter(SimpleStream6703::primeNumber)
                .sorted()
                .peek(n -> System.out.print(n + " "))
                .sum();
        System.out.println("="+sum);
    }

    //找出其中的素数
    private static boolean primeNumber(int x) {
        if (x == 1) {
            return true;
        } else if (x % 2 == 0) {
            return false;
        } else {
            for (int i = 3; i * i <= x; i += 2) {
                if (x % 2 == 0) {
                    return false;
                }
            }
            return true;
        }
    }
}